<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="告警级别" prop="alarmLevel">
        <dict-tag :type="DICT_TYPE.ALARM_LEVEL" :value="formData.alarmLevel" />
      </el-form-item>
      <el-form-item label="告警时间" prop="alarmTime">
        <el-date-picker
          v-model="formData.alarmTime"
          type="date"
          value-format="x"
          placeholder="选择告警时间"
          style="width: 100%"
          disabled
        />
      </el-form-item>
      <el-form-item label="告警内容" prop="alarmContent">
        <el-input type="textarea" v-model="formData.alarmContent" height="150px" disabled />
      </el-form-item>
      <el-form-item label="被监测设备" prop="monitorDeviceName">
        <el-input v-model="formData.monitorDeviceName" disabled />
      </el-form-item>
      <el-form-item label="终端设备" prop="terminalDeviceNames">
        <el-input v-model="formData.terminalDeviceNames" disabled />
      </el-form-item>
      <!-- <el-form-item label="任务名称" prop="taskName">
        <el-input v-model="formData.taskName" placeholder="请输入任务名称" />
      </el-form-item>
      <el-form-item label="规则名称" prop="ruleName">
        <el-input v-model="formData.ruleName" placeholder="请输入规则名称" />
      </el-form-item>
      <el-form-item label="触发器名称" prop="alarmTriggerName">
        <el-input v-model="formData.alarmTriggerName" placeholder="请输入触发器名称" />
      </el-form-item> -->
      <el-form-item label="状态" prop="state">
        <el-select v-model="formData.state" placeholder="请选择状态">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.VMIOT_ALARM_RECORD_STATE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理意见" prop="remark">
        <el-input type="textarea" v-model="formData.remark" placeholder="请输入处理意见" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">保 存</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { AlarmRecordApi, AlarmRecordVO } from '@/api/vmiot/alarmrecord'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'

/** 告警记录 表单 */
defineOptions({ name: 'AlarmRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  alarmLevel: 0,
  alarmTime: undefined,
  alarmContent: undefined,
  signalIds: undefined,
  monitorDeviceId: undefined,
  monitorDeviceOutId: undefined,
  monitorDeviceName: undefined,
  terminalDeviceIds: undefined,
  terminalDeviceOutIds: undefined,
  terminalDeviceNames: undefined,
  taskId: undefined,
  taskName: undefined,
  ruleId: undefined,
  ruleName: undefined,
  alarmTriggerId: undefined,
  alarmTriggerName: undefined,
  state: undefined,
  stateChangeTime: undefined,
  remark: undefined
})
const formRules = reactive({
  state: [{ required: true, message: '请选择状态', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = '告警详情'
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await AlarmRecordApi.getAlarmRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as AlarmRecordVO
    await AlarmRecordApi.updateAlarmRecordState(data)
    message.success(t('common.updateSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    alarmLevel: 0,
    alarmTime: undefined,
    alarmContent: undefined,
    signalIds: undefined,
    monitorDeviceId: undefined,
    monitorDeviceOutId: undefined,
    monitorDeviceName: undefined,
    terminalDeviceIds: undefined,
    terminalDeviceOutIds: undefined,
    terminalDeviceNames: undefined,
    taskId: undefined,
    taskName: undefined,
    ruleId: undefined,
    ruleName: undefined,
    alarmTriggerId: undefined,
    alarmTriggerName: undefined,
    state: undefined,
    stateChangeTime: undefined,
    remark: undefined
  }
  formRef.value?.resetFields()
}
</script>
